<template>
  <div id='create_tag'>
    <Row type='flex' align-item='middle' justify='center' style="padding-top: 100px;">
      <Col>
        <Form ref="formInline" :model="formInline" :rules="ruleInline" :label-width="120">
          <FormItem prop="group" class="form-content-top" label='标签分组：'>
            <Select v-model="formInline.group" style="width:280px">
              <Option v-for="item in groupList" :value="item.shopTagId" :key="item.shopTagId">{{ item.shopTagName }}</Option>
            </Select>
          </FormItem>
          <FormItem prop="tagName" class="form-content-top" label='标签：'>
            <Input type="text" v-model="formInline.tagName" style="width:280px" placeholder="请输入" :maxlength="16" @on-keyup='filterName' @on-blur="filterName"></Input>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="handleSubmit" style='width:100px'>确定</Button>
            <Button style="margin-left: 15px;width:100px" @click="handleCancel">取消</Button>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </div>
</template>

<script>
import { groupList, createTag } from '@/api/system/tag'
import { routeEqual } from '@/libs/util'
export default {
  name: 'create_tag',
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (vm.groupId !== to.query.id) {
        vm.formInline.group = to.query.id
        vm.groupId = to.query.id
      }
    })
  },
  data () {
    return {
      groupId: '',
      submitLock: false,
      groupList: [],
      formInline: {
        group: '',
        tagName: ''
      },
      ruleInline: {
        group: { required: true, message: '请选择标签分组', trigger: 'change' },
        tagName: { required: true, message: '请输标签名称', trigger: 'change' }
      }
    }
  },
  mounted () {
    this.formInline.group = this.$route.query.id ? this.$route.query.id : ''
    this.groupId = this.$route.query.id ? this.$route.query.id : ''
    this.getList()
  },
  methods: {
    filterName () {
      this.formInline.tagName = this.formInline.tagName.replace(/[^A-Za-z\u4e00-\u9fa5]/, '')
    },
    handleSubmit () {
      this.$refs.formInline.validate(valid => {
        if (valid) {
          if (this.submitLock) {
            return
          }
          this.submitLock = true
          createTag({
            merchantId: this.$store.state.user.merchantId,
            shopTagName: this.formInline.tagName,
            parentId: this.formInline.group,
            level: 2,
            creator: this.$store.state.user.userName
          }).then(res => {
            this.submitLock = false
            if (res.data.status === 200) {
              this.$Message.success('新增成功！')
              this.handleCancel()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    handleCancel () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        if (this.groupId) {
          this.$router.push({
            name: 'store_tag', query: { id: this.groupId }
          })
        } else {
          this.$router.push({
            name: 'store_tag'
          })
        }
      })
    },
    // 获取标签列表
    getList () {
      groupList({
        merchantId: this.$store.state.user.merchantId,
        shopTagName: '',
        level: 1,
        offset: 0,
        limit: 999999
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.groupList = result.data || []
        } else {
          this.$Message.error(`${result.error}`)
        }
      })
    }
  }
}
</script>
